package org.qpcr.community.device.web;


import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.ezorm.core.param.QueryParam;
import org.hswebframework.ezorm.rdb.mapping.defaults.SaveResult;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.QueryAction;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.authorization.annotation.SaveAction;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.hswebframework.web.exception.NotFoundException;
import org.qpcr.community.device.entity.DeviceRunTimeEntity;
import org.qpcr.community.device.response.DeviceDetail;
import org.qpcr.community.device.service.DeviceRunTimeService;
import org.qpcr.community.elastic.search.service.ElasticSearchService;
import org.qpcr.community.elastic.search.service.reactive.ReactiveElasticSearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Map;
import java.util.function.Function;


@RestController
@RequestMapping("/device/runTime")
@Authorize
@Resource(id="device-runTime",name = "设备运行时长")
@Slf4j
@Tag(name = "设备运行时长")
@RequiredArgsConstructor
public class DeviceRunTimeController implements ReactiveServiceCrudController<DeviceRunTimeEntity, String>{

    @Getter
    private final DeviceRunTimeService service;

    @Autowired
    private final ReactiveElasticSearchService elasticSearchService;


    @PostMapping("/no-auth")
    @SaveAction
    @Operation(
        summary = "新增"
    )
    @Authorize(ignore = true)
    public Mono<SaveResult> addOverride(@RequestBody Flux<DeviceRunTimeEntity> payload) {
        return this.save(payload);
    }

    @GetMapping("/{id:.+}/detail")
    @QueryAction
    @Operation(
        summary = "根据ID查询详情"
    )
    @Authorize(ignore = true)
    public Mono<DeviceRunTimeEntity> getByDeviceId(@PathVariable @Parameter(description = "设备ID") String id) {

        return service.findById(id) ;
    }

    @GetMapping("/es/{index:.+}/detail")
    @QueryAction
    @Operation(
        summary = "根据ID查询详情"
    )
    @Authorize(ignore = true)
    public <T> Flux<T> getEsById(@PathVariable @Parameter(description = "索引ID") String index, QueryParam queryParam, Function<Map<String, Object>, T> mapper) {



        return elasticSearchService.query( index,  queryParam, mapper) ;
    }


}
